import { observationPoolDetail } from "@/services";

export default {
  namespace: "obsPool",
  state: {
    reportDetail: { zz500Model: {}, hs300Model: {}, otherList: [] },
    noData: false
  },
  effects: {
    *asyncObservationPoolDetail({ payload }, { put }) {
      const ret = yield observationPoolDetail({ ...payload });
      if (ret.code == 200) {
        let result = { zz500Model: {}, hs300Model: {}, otherList: [] };
        let noData = false;
        let data = ret.data.detail;
        if (data.length) {
          data.forEach(item => {
            if (item.indexName == "中证500") {
              result.zz500Model = item;
            } else if (item.indexName == "沪深300") {
              result.hs300Model = item;
            } else {
              result.otherList.push(item);
            }
          });
        } else {
          noData = true;
        }
        yield put({
          type: "save",
          payload: {
            reportDetail: result,
            noData
          }
        });
      } else {
        let result = { zz500Model: {}, hs300Model: {}, otherList: [] };
        let noData = true;
        yield put({
          type: "save",
          payload: {
            reportDetail: result,
            noData
          }
        });
      }
    }
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    }
  }
};
